package services;

import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.openjpa.persistence.PersistenceException;

public class DAOFactoryOpenJpa extends DAOFactory {

	private EntityManagerFactory emf;
	
	public DAOFactoryOpenJpa() {
		super();
		try {
			initializeEntityManagerFactory();
		} catch (PersistenceException e) {
			throw new RuntimeException(e);
		}
	}

	private void initializeEntityManagerFactory() throws PersistenceException {
		try {
			Properties props = new Properties();
			props.put("openjpa.ConnectionURL", "jdbc:postgresql://localhost:5432/editor");
			props.put("openjpa.ConnectionDriverName", "org.postgresql.Driver");
			props.put("openjpa.ConnectionUserName", "postgres");
			props.put("openjpa.ConnectionPassword", "123456");
			props.put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
			props.put("openjpa.RuntimeUnenhancedClasses", "supported");
			emf =Persistence.createEntityManagerFactory("test", props);
		} catch (Exception e) {
			throw new PersistenceException("Failed to create EntityManagerFactory!", null, e, false);
		}
    }

	//@Override
	public IUserService getUserService() {
		return new UserService(emf);
	}

	//@Override
	public IImageService getImageService() {
		return new ImageService(emf);
	}

}
